package com.pureut.production.controller;

import com.pureut.common.core.web.controller.BaseController;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.core.web.page.TableDataInfo;
import com.pureut.common.log.annotation.Log;
import com.pureut.common.log.enums.BusinessType;
import com.pureut.common.security.annotation.RequiresPermissions;
import com.pureut.production.domain.dto.ProcessCardLogDto;
import com.pureut.production.domain.vo.ProcessCardLogVo;
import com.pureut.production.service.ProcessCardLogService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.List;


/**
 * @Author: YSL
 * @Date: 2023-02-16 14:42:27
 * @Desc: 工艺卡日志Controller
 */
@RestController
@RequestMapping("/processCardLog")
public class ProcessCardLogController extends BaseController {

    /**
     * 工艺卡日志Service
     */
    @Resource
    private ProcessCardLogService processCardLogService;

    /**
     * 查询工艺卡日志列表
     */
    @RequiresPermissions("productionTube:processmage:processcardlog:list")
    @GetMapping("/list")
    public TableDataInfo list(ProcessCardLogVo processCardLogVo) {
        startPage();
        List<ProcessCardLogDto> processCardLogDtos = processCardLogService.selectProcessCardLogList(processCardLogVo);
        return getDataTable(processCardLogDtos);
    }

    /**
     * 新增工艺卡日志
     */
//    @RequiresPermissions("production:processCardLog:add")
    @Log(title = "新增工艺卡日志", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@Validated @RequestBody ProcessCardLogVo processCardLogVo) {
        return toAjax(processCardLogService.insertProcessCardLog(processCardLogVo));
    }

    /**
     * 工艺卡查看
     */
    @RequiresPermissions("productionTube:processmage:processcardlog:query")
    @GetMapping("/view")
    public AjaxResult view(Long id) {
        return AjaxResult.success(processCardLogService.view(id));
    }

    /**
     * 工艺卡导出
     */
    @PostMapping("/derive")
    public void outData(HttpServletResponse response, ProcessCardLogVo processCardLogVo) throws IOException {

        XSSFWorkbook sheets = processCardLogService.outputExcl(processCardLogVo);
        try {
            response.setCharacterEncoding("utf-8");
            LocalDate now = LocalDate.now();
            String fileName = now + "工艺卡日志.xlsx";
            response.setContentType("application/octet-stream;charset=UTF-8");
            response.setHeader( "content-Disposition",  "attachment; filename=" + URLEncoder.encode(fileName,  "UTF-8"));
            ServletOutputStream outputStream = response.getOutputStream();
            sheets.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
